/*
 * @FileName: [UserController.java]
 * 
 * @Package controller
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com. All rights reserved.
 * 
 * This software is the confidential and proprietary information of BCINFO Technology Limited Company ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the contract agreement you entered into with RKY.
 * 
 * $Rev: 2909 $ $LastChangedDate: 2013-10-12 10:19:36 +0800 (周六, 2013-10-12) $ $LastChangedBy: fanb $
 * 
 * @category bcinfo
 * 
 * @version 1.1
 * 
 * @author $Author: fanb $
 * 
 * Change History:[Formatter: author date description] <br/> 1 2 3
 */

package com.bci.pwtz.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.admin.service.AdminDictionaryService;
import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.CompanyUser;
import com.bci.pwtz.mysql.model.Dictionary;
import com.bci.pwtz.mysql.model.DictionaryType;
import com.bci.pwtz.service.CompanyUserService;



/**
 * 字典管理controller <br/>
 */
@Controller
@RequestMapping("/admin/dictionary")
public class AdminDictionaryController extends AdminAbstractController
{
    @Autowired
    private AdminDictionaryService adminDictionaryService;
    @Autowired
    private CompanyUserService companyUserService;
    
    @RequestMapping("/selectTypeByParent")
    @ResponseBody
    public List<DictionaryType> selectDictionaryTypeByParent(Long pid){
    	try {
			return adminDictionaryService.selectDictionaryTypeByParent(pid);
		} catch (PwtzDatabaseException e) {
			e.printStackTrace();
		}
    	return null;
    }
    
    @RequestMapping("/list")
    @ResponseBody
    public PaginationList<Dictionary> findDictionary(Long pid,
			Long deviceType,Integer status, int pageSize, int pageNo){
    	
    	try {
			return adminDictionaryService.findDictionary(pid, deviceType,status, pageSize, pageNo);
		} catch (PwtzDatabaseException e) {
			handleException(e);
		}
    	return null;
    }
    @RequestMapping("/load")
    @ResponseBody
    public Map<String,Object> load(Long id){
    	try {
    		Dictionary bean = adminDictionaryService.load(id);
    		if(bean != null){
    			Map<String,Object> map = new HashMap<String, Object>();
    			map.put("dictionaryId", bean.getDictionaryId());
    			map.put("deviceName", getDeviceName(bean.getDictionaryTypeId()));
    			map.put("dictionaryTypeId", bean.getDictionaryTypeId());
    			map.put("dictionaryTypeName", PwtzConstants.DICTIONARY_TYPE.get(bean.getDictionaryTypeId()));
    			map.put("dictionaryValue", bean.getDictionaryValue());
    			map.put("createor", "");
    			if(bean.getCreatorId() != null){
    				CompanyUser user = companyUserService.load(bean.getCreatorId());
    				if(user != null){
    					map.put("createor", user.getUserName());
    				}
    			}
    			return map;
    		}
    	} catch (PwtzDatabaseException e) {
    		handleException(e);
    	}
    	return null;
    }
    
    @RequestMapping("/approve")
    @ResponseBody
    public Map<String,String> approve(Long id,int status){
    	try {
    		Dictionary bean = adminDictionaryService.load(id);
			int row = adminDictionaryService.approveDictionary(id, status);
			 
			if(row > 0){
				setLog(3, "审核字典【"+getDeviceName(bean)+"】【"+getDictionaryTypeName(bean)+"】【"+bean.getDictionaryValue()+"】", getUserId());
				return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null,"审核成功");
			}
		} catch (Exception e) {
			handleException(e);
		}
		return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_INFO, null,"审核失败");
    }
    
    @RequestMapping("/add")
    @ResponseBody
    public Map<String,String> add(Dictionary bean){
    	try {
    		bean.setStatus(1);
			bean = adminDictionaryService.add(bean);
			setLog(3, "添加字典【"+getDeviceName(bean)+"】【"+getDictionaryTypeName(bean)+"】【"+bean.getDictionaryValue()+"】", getUserId());
			return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null,"添加成功");
		} catch (Exception e) {
			handleException(e);
		}
		return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_INFO, null,"添加失败");
    }
    
    @RequestMapping("/update")
    @ResponseBody
    public Map<String,String> update(Dictionary bean){
    	try {
			int row = adminDictionaryService.update(bean);
			bean = adminDictionaryService.load(bean.getDictionaryId());
			if(row > 0){
				setLog(3, "修改字典【"+getDeviceName(bean)+"】【"+getDictionaryTypeName(bean)+"】【"+bean.getDictionaryValue()+"】", getUserId());
				return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null,"修改成功"); 
			}
		} catch (Exception e) {
			handleException(e);
		}
		return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_INFO, null,"修改失败");
    }
    
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,String> delete(Long id){
    	try {
    		Dictionary bean = adminDictionaryService.load(id);
    		int row = adminDictionaryService.delete(id);
    		if(row > 0){
    			setLog(3, "删除字典【"+getDeviceName(bean)+"】【"+getDictionaryTypeName(bean)+"】【"+bean.getDictionaryValue()+"】", getUserId());
    			return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO, null,"删除成功"); 
    		}
    	} catch (Exception e) {
    		handleException(e);
    	}
    	return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_INFO, null,"删除失败");
    }
    
    private String getDeviceName(Long dictionaryType){
    	//因存在三级关系，此变量记录三级关系
    	Map<Long,Long> special = new HashMap<Long, Long>();
    	special.put(200L, 118L);
    	special.put(201L, 118L);
    	special.put(202L, 118L);
    	special.put(203L, 118L);
    	special.put(204L, 118L);
    	special.put(205L, 118L);
    	special.put(206L, 118L);
    	special.put(207L, 118L);
    	special.put(208L, 118L);
    	special.put(209L, 118L);
    	special.put(210L, 118L);
    	
    	String deviceType = String.valueOf(dictionaryType);
    	if(deviceType != null && deviceType.length()>0){
    		deviceType =  deviceType.substring(0, deviceType.length()-3);
    		dictionaryType = Long.valueOf(deviceType);
    		if(special.containsKey(dictionaryType)){
    			dictionaryType = special.get(dictionaryType); 
    		}
    		return PwtzConstants.DEVICE_TYPE_MAP.get(dictionaryType);
    	}
    	return null;
    }
    
    private String getDeviceName(Dictionary bean){
    	if(bean!=null){
    		return getDeviceName(bean.getDictionaryTypeId());
    	}
    	return null;
    }
    
    private String getDictionaryTypeName(Dictionary bean){
    	if(bean != null){
    		Long dictionaryType =  bean.getDictionaryTypeId();
    		return PwtzConstants.DICTIONARY_TYPE.get(dictionaryType);
    	}
    	return null;
    }
    
}